package org.zjx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.zjx.dao.entity.DeviceDailyTask;
import org.zjx.dao.mapper.DeviceDailyTaskMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.zjx.service.IDeviceDailyTaskService;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-05-28
 */
@Service
public class DeviceDailyTaskServiceImpl extends ServiceImpl<DeviceDailyTaskMapper, DeviceDailyTask> implements IDeviceDailyTaskService {

    @Override
    public DeviceDailyTask selectByConditions(long deviceId, int temporary) {
        QueryWrapper<DeviceDailyTask> wrapper = new QueryWrapper<>();
        wrapper.eq("ddt.temporary", temporary)      // 设备任务临时标识
                .eq("ddt.device_id", deviceId)   // 目标账号
                .eq("dt.status", 3);                     // 任务状态为3

        // 3. 执行查询
        List<DeviceDailyTask> tasks = getBaseMapper().selectByConditions(wrapper);
        return tasks.isEmpty() ? null : tasks.get(0);
    }

    /**
     * 新增UPSERT方法（根据device_id + temporary判断）
     * @param dailyTaskId 任务ID
     * @param deviceId 设备ID
     * @param temporary 临时标识
     * @return 是否操作成功
     */
    @Override
    @Transactional
    public boolean updateByConditions(long dailyTaskId, long deviceId, int temporary) {
        // 调用自定义SQL实现UPSERT
        int affectedRows = getBaseMapper().upsertDeviceTask(dailyTaskId, deviceId, temporary);
        return affectedRows > 0;
    }
}
